package com.geostar.constructionland.analyse.service.spatiaanalyst.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.geostar.constructionland.analyse.entity.Block;
import com.geostar.constructionland.analyse.entity.alyresult.*;
import com.geostar.constructionland.analyse.entity.alyresult.tzhdlgdfx.AlyResultDlfx;
import com.geostar.constructionland.analyse.entity.model.AnalyseModel;
import com.geostar.constructionland.analyse.mapper.AlyResultYjjbntMapper;
import com.geostar.constructionland.analyse.mapper.sptalyresult.*;
import com.geostar.constructionland.analyse.service.spatiaanalyst.SptAlyParamService;
import com.geostar.constructionland.common.dao.YzGeoJsonDao;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static com.geostar.constructionland.common.global.ConstAnalyseType.*;

/**
 * @author yelll
 * @since 2020-5-27 11:37
 * 通用分析
 **/
@Component
@Slf4j
public class KjccSptAnalyse extends NormalSptAnalyst {
    @Autowired
    SptAlyParamService sptAlyParamService;
    @Autowired
    private YzGeoJsonDao yzGeoJsonDao;
    @Autowired
    private AlyResultDlfxMapper alyResultDlfxMapper;
    @Autowired
    private AlyResultGhdlMapper alyResultGhdlMapper;
    @Autowired
    private AlyResultJsydgzqMapper alyResultJsydgzqMapper;
    @Autowired
    private AlyResultXzqMapper alyResultXzqMapper;
    @Autowired
    private AlyResultYpysMapper alyResultYpysMapper;
    @Autowired
    private AlyResultGbzntMapper alyResultGbzntMapper;
    @Autowired
    private AlyResultYjjbntMapper alyResultYjjbntMapper;
    @Autowired
    private AlyResultYphxMapper alyResultYphxMapper;
    @Autowired
    private AlyResultCszbyjjbntMapper alyResultCszbyjjbntMapper;
    @Autowired
    private AlyResultDqhpMapper alyResultDqhpMapper;
    @Autowired
    private AlyResultSjxmfxMapper alyResultSjxmfxMapper;
    @Autowired
    private AlyResultXinjianquMapper alyResultXinjianquMapper;
    @Autowired
    private AlyResultLystbhhxMapper alyResultLystbhhxMapper;
    @Autowired
    private AlyResultHystbhhxMapper alyResultHystbhhxMapper;

    @Override
    public Map<String, Object> getQueryParam(AnalyseModel analyseModel) throws Exception {
        /**确定接口参数构造类型**/
        Map<String, Object> requestInfo = sptAlyParamService.getCommonParam(analyseModel);
        Map<String, Object> param = new HashMap<>(1);
        try {


            requestInfo.put("EraseBlocks", queryHghxFxData(analyseModel.getOriginFxlx(), analyseModel.getDlIds()));


            param.put("requestInfo", objectMapper.writeValueAsString(requestInfo));
            log.info("requestInfo:" + objectMapper.writeValueAsString(requestInfo));
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
        return param;
    }


    private List<Block> queryHghxFxData(String fxlx, String dlIds) {
        dlIds = dlIds == null ? "" : dlIds;
        String[] hxgxArr = dlIds.split(",");
        return queryBlock(fxlx, hxgxArr);
    }

    private List<Block> queryBlock(String fxlx, String[] fxids) {
        List<Block> blockList = new ArrayList<>();
        if (StringUtils.isBlank(fxlx)) {
            return null;
        }

        if (CONST_GHDLFX.equals(fxlx)) {        // 规划地类分析
            List<AlyResultGhdl> resultList =
                    alyResultGhdlMapper.selectList(new LambdaQueryWrapper<AlyResultGhdl>().in(AlyResultGhdl::getFxid, fxids));
            resultList.forEach(alyResultGhdl -> {
                Block block = new Block();
                block.setDk_id(alyResultGhdl.getYdkbs());
                block.setGeoJson(alyResultGhdl.getRealYzGeoJson());
                blockList.add(block);
            });
        } else if (CONST_JSYDGZQFX.equals(fxlx)) {        // 管制区分析
            List<AlyResultJsydgzq> resultList =
                    alyResultJsydgzqMapper.selectList(new LambdaQueryWrapper<AlyResultJsydgzq>().in(AlyResultJsydgzq::getFxid, fxids));
            resultList.forEach(alyResultJsydgzq -> {
                Block block = new Block();
                block.setDk_id(alyResultJsydgzq.getDkbs());
                block.setGeoJson(alyResultJsydgzq.getYzgeojson());
                blockList.add(block);
            });
        } else if (CONST_TZHDLFX.equals(fxlx)) {        // 调整后地类分析
            List<AlyResultDlfx> resultList =
                    alyResultDlfxMapper.selectList(new LambdaQueryWrapper<AlyResultDlfx>().in(AlyResultDlfx::getDlid, fxids));
            resultList.forEach(alyResultDlfx -> {
                Block block = new Block();
                block.setDk_id(alyResultDlfx.getDkbs());
                if (StringUtils.isNoneBlank(alyResultDlfx.getMongoId())) {
                    block.setGeoJson(yzGeoJsonDao.queryYzGeoJson(alyResultDlfx.getMongoId()).getGeoJson());
                } else {
                    block.setGeoJson(alyResultDlfx.getRealYzgeoJson());
                }
                blockList.add(block);
            });
        } else if (CONST_XZQFX.equals(fxlx)) {        // 行政区分析
            List<AlyResultXzq> resultList =
                    alyResultXzqMapper.selectList(new LambdaQueryWrapper<AlyResultXzq>().in(AlyResultXzq::getAlyId, fxids));
            resultList.forEach(alyResultDlfx -> {
                Block block = new Block();
                block.setDk_id(alyResultDlfx.getLandCode());
                if (StringUtils.isNoneBlank(alyResultDlfx.getMongoId())) {
                    block.setGeoJson(yzGeoJsonDao.queryYzGeoJson(alyResultDlfx.getMongoId()).getGeoJson());
                } else {
                    block.setGeoJson(alyResultDlfx.getRealYzGeoJson());
                }
                blockList.add(block);
            });
        } else if (CONST_YPYSFX.equals(fxlx)) {        // 已批预审分析
            List<AlyResultYpys> resultList =
                    alyResultYpysMapper.selectList(new LambdaQueryWrapper<AlyResultYpys>().in(AlyResultYpys::getFxid, fxids));
            resultList.forEach(alyResultYpys -> {
                Block block = new Block();
                block.setDk_id(alyResultYpys.getDkbs());
                block.setGeoJson(alyResultYpys.getYzgeojson());
                blockList.add(block);
            });
        } else if (CONST_GBZNTFX.equals(fxlx)) {        // 高标准农田分析
            List<AlyResultGbznt> resultList =
                    alyResultGbzntMapper.selectList(new LambdaQueryWrapper<AlyResultGbznt>().in(AlyResultGbznt::getFxid, fxids));
            resultList.forEach(alyResultGbznt -> {
                Block block = new Block();
                block.setDk_id(alyResultGbznt.getDkbs());
                block.setGeoJson(alyResultGbznt.getYzgeojson());
                blockList.add(block);
            });
        } else if (CONST_YJJBNTFX.equals(fxlx)) {        // 高标准农田分析
            List<AlyResultYjjbnt> resultList =
                    alyResultYjjbntMapper.selectList(new LambdaQueryWrapper<AlyResultYjjbnt>().in(AlyResultYjjbnt::getFxid, fxids));
            resultList.forEach(alyResultYjjbnt -> {
                Block block = new Block();
                block.setDk_id(alyResultYjjbnt.getDkbs());
                block.setGeoJson(alyResultYjjbnt.getYzgeojson());
                blockList.add(block);
            });
        } else if (CONST_YPHXFX.equals(fxlx)) {        // 高标准农田分析
            List<AlyResultYphx> resultList =
                    alyResultYphxMapper.selectList(new LambdaQueryWrapper<AlyResultYphx>().in(AlyResultYphx::getFxid, fxids));
            resultList.forEach(alyResultYphx -> {
                Block block = new Block();
                block.setDk_id(alyResultYphx.getDkbs());
                block.setGeoJson(alyResultYphx.getYzgeojson());
                blockList.add(block);
            });
        } else if (CONST_SJGZXMFX.equals(fxlx)) {        // 三旧改造项目分析
            List<AlyResultSjgzxm> resultList =
                    alyResultSjxmfxMapper.selectList(new LambdaQueryWrapper<AlyResultSjgzxm>().in(AlyResultSjgzxm::getFxid, fxids));
            resultList.forEach(alyResultSjgzxm -> {
                Block block = new Block();
                block.setDk_id(alyResultSjgzxm.getDkbs());
                block.setGeoJson(alyResultSjgzxm.getYzgeojson());
                blockList.add(block);
            });
        } else if (CONST_CSZBYJJBNTFX.equals(fxlx)) {        // 永久基本农田占压分析（考虑城市周边线）分析
            List<AlyResultCszbyjjbnt> resultList =
                    alyResultCszbyjjbntMapper.selectList(new LambdaQueryWrapper<AlyResultCszbyjjbnt>().in(AlyResultCszbyjjbnt::getFxid, fxids));
            resultList.forEach(alyResultCszbyjjbnt -> {
                Block block = new Block();
                block.setDk_id(alyResultCszbyjjbnt.getDkbs());
                block.setGeoJson(alyResultCszbyjjbnt.getYzgeojson());
                blockList.add(block);
            });
        } else if (CONST_JXQFX.equals(fxlx)) {        // 建新区分析
            List<AlyResultXinjianqu> resultList =
                    alyResultXinjianquMapper.selectList(new LambdaQueryWrapper<AlyResultXinjianqu>().in(AlyResultXinjianqu::getFxid, fxids));
            resultList.forEach(alyResultXinjianqu -> {
                Block block = new Block();
                block.setDk_id(alyResultXinjianqu.getDkid());
                block.setGeoJson(alyResultXinjianqu.getYzgeojson());
                blockList.add(block);
            });
        } else if (CONST_DQHPFX.equals(fxlx)) {        // 低丘缓坡分析
            List<AlyResultDqhp> resultList =
                    alyResultDqhpMapper.selectList(new LambdaQueryWrapper<AlyResultDqhp>().in(AlyResultDqhp::getFxid, fxids));
            resultList.forEach(alyResultDqhp -> {
                Block block = new Block();
                block.setDk_id(alyResultDqhp.getDkid());
                block.setGeoJson(alyResultDqhp.getYzgeojson());
                blockList.add(block);
            });
        } else if(CONST_LYSTBHHXFX.equals(fxlx)) {
            List<AlyResultLystbhhx> resultList =
                    alyResultLystbhhxMapper.selectList(new LambdaQueryWrapper<AlyResultLystbhhx>().in(AlyResultLystbhhx::getFxid, fxids));
            resultList.forEach(alyResultLystbhhx -> {
                Block block = new Block();
                block.setDk_id(alyResultLystbhhx.getDkid());
                block.setGeoJson(alyResultLystbhhx.getYzgeojson());
                blockList.add(block);
            });
        } else if(CONST_HYSTBHHXFX.equals(fxlx)) {
            List<AlyResultHystbhhx> resultList =
                    alyResultHystbhhxMapper.selectList(new LambdaQueryWrapper<AlyResultHystbhhx>().in(AlyResultHystbhhx::getFxid, fxids));
            resultList.forEach(alyResultHystbhhx -> {
                Block block = new Block();
                block.setDk_id(alyResultHystbhhx.getDkid());
                block.setGeoJson(alyResultHystbhhx.getYzgeojson());
                blockList.add(block);
            });
        }
        return blockList;
    }
}
